package com.marver.exception;

import com.marver.common.result.Result;
import com.marver.common.result.ResultStatus;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: web 异常捕获
 * @Author: Marver
 * @date: 2018-04-27 19:29
 **/
@RestControllerAdvice
public class HmExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(DuplicateKeyException.class)
    public Result handleDuplicateKeyException(DuplicateKeyException e){
        logger.error(e.getMessage(), e);
        return new Result(ResultStatus.FAIL.getErrorCodeToString(),"数据库中已存在该记录");
    }

    @ExceptionHandler(AuthorizationException.class)
    public Result handleAuthorizationException(AuthorizationException e){
        logger.error(e.getMessage(), e);
        return new Result(ResultStatus.FAIL.getErrorCodeToString(),"没有权限，请联系管理员授权");
    }
}
